from django.shortcuts import render

# Create your views here.


from .models import CourseCategory, Course, CourseChapter
from rest_framework.viewsets import GenericViewSet
from rest_framework.mixins import ListModelMixin
from .serializer import CourseCategorySerializer, CourseSerializer, CourseChapterSerializer
from utils.common_mixin import CommonListModelMixin as ListModelMixin
from utils.common_mixin import CommonRetrieveModelMixin as RetrieveModelMixin
from .pagination import CommonPagination
from rest_framework.filters import OrderingFilter
from django_filters.rest_framework import DjangoFilterBackend


class CourseCategoryView(GenericViewSet, ListModelMixin):
    # 查询所有
    queryset = CourseCategory.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseCategorySerializer


class CourseView(GenericViewSet, ListModelMixin, RetrieveModelMixin):
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    serializer_class = CourseSerializer
    pagination_class = CommonPagination
    # 排序，过滤课程分类
    filter_backends = [OrderingFilter, DjangoFilterBackend]
    ordering_fields = ['students', 'price']
    filterset_fields = ['course_category']


class CourseChapterView(GenericViewSet, ListModelMixin):
    serializer_class = CourseChapterSerializer
    queryset = CourseChapter.objects.filter(is_delete=False, is_show=True).order_by('orders')
    filter_backends = [DjangoFilterBackend]
    filterset_fields = ['course']  # 按课程过滤


from utils.common_response import APIResponse
from rest_framework.filters import SearchFilter


# 搜素就是查询所有，带过滤，按课程名搜素
class SearchCourseView(GenericViewSet, ListModelMixin):
    serializer_class = CourseSerializer
    queryset = Course.objects.filter(is_delete=False, is_show=True).order_by('orders')
    pagination_class = CommonPagination
    filter_backends = [SearchFilter]
    search_fields = ['name', 'brief']
    # 1 brief内容非常多，如果使用mysql的搜索，like 全文搜，
    # 随着数据量越来越大，搜索越来越慢---》分布式全文检索引擎elasticsearch
